var userId = '';
axios.defaults.withCredentials=true;    //发送请求携带session信息
//完整生成顶部导航栏
function initNavHeader(itemName) {
    $('.cinema-header-container').html(generateHeaderNavHtml());
    setNavHighlight(itemName);
    setUserIdBlockMenu();

}

//生成顶部导航栏代码
function generateHeaderNavHtml() {
    var innerHTML =
        '<div class="cinema-header layui-row">\n' +
        '    <div class="layui-col-md2 layui-col-sm2">\n' +
        '        <a href="" class="total-icon">电影购票系统</a>\n' +
        '    </div>\n' +
        '    <div class="layui-col-md6 layui-col-sm7">\n' +
        '        <ul class="layui-nav cinema-nav">\n' +
        '            <li class="layui-nav-item film-search-item"><a href="./FilmSearchPage.html">信息查询</a></li>\n' +
        '            <li class="layui-nav-item book-ticket-item"><a href="./BookTicketPage.html">购票</a></li>\n' +
        '            <li class="layui-nav-item my-order-item"><a href="./MyOrderPage.html">我的订单</a></li>\n' +
        '            <li class="layui-nav-item"><a href="https://www.bupt.edu.cn/" target="_blank">关于我们</a></li>\n' +
        '        </ul>\n' +
        '    </div>' +
        '    <div class="layui-col-md2 layui-col-md-offset2 layui-col-sm2 layui-col-sm-offset1">' +
        '        <a id="user-id-block" href="javascript:;">' +
        '           <i class="layui-icon layui-icon-username"></i> 用户名 <i class="layui-icon layui-icon-triangle-d"></i>' +
        '        </a>' +
        '    </div>' +
        '</div>'
    return innerHTML;
}

//设置顶部导航栏高亮链接
function setNavHighlight(itemName) {
    $('.' + itemName).addClass('layui-this');
}

//设置登出菜单
function setUserIdBlockMenu() {
    layui.use('dropdown', function () {
        var dropdown = layui.dropdown;
        dropdown.render({
            elem: '#user-id-block',
            trigger: 'hover',
            data: [{
                title: '退出登录',
                templet: '<i class="layui-icon layui-icon-logout"></i> {{d.title}} ',
                id: 100
            }],
            style: 'width: 120px;margin-top: 25px;',
            click: function (obj) {
                // layer.msg('点击了：'+ obj.title)
                //退出登录逻辑
                console.log(obj);
                switch (obj.id) {
                    case 100:
                        logout();
                        break;

                }
            }
        })
    })
}

//在刷新页面时，检查是否已经登录
function checkLogin() {
    // axios.defaults.withCredentials=true;    //发送请求携带session信息
    axios({
        method: 'get',
        url: 'http://localhost:9090/user/getUserFromSession',
    }).then(res=>{
        console.log(res);
        if (res.data.status !== 0) {
            $(location).attr('href', './LoginPage.html');
        }
        else {
            $('#user-id-block').html(
                '<i class="layui-icon layui-icon-username"></i> '+ res.data.data.userId + '<i class="layui-icon layui-icon-triangle-d"></i>'
            );
            //给页面存储userId的值
            userId = res.data.data.userId;
        }
    });

}

//退出登录
function logout() {
    axios({
        method: 'get',
        url: 'http://localhost:9090/user/logout',
    }).then(res=>{
        if (res.data.status === 0) {
            $(location).attr('href', './LoginPage.html');
        } else {
            layer.alert('出现异常,请稍后再试');
        }
    });
}

//可以在url中指定某个参数，调用这个
function getQueryVariable(variable)
{
    // console.log(window.location.href);   //http://localhost:63343/210416_software_develop_cinema_web/FilmSearchPage.html?id=dfdsf
    // console.log(window.location.href.split('?')[0]);    //http://localhost:63343/210416_software_develop_cinema_web/FilmSearchPage.html
    // console.log(window.location.search);    //?id=dfdsf     //null
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if(pair[0] === variable){return pair[1];}
    }
    // return(false);
    return null;
}